<?php

function d4($i)
{
    if ($i < 10)
    {
        return "000" . $i;
    }
    if ($i < 100)
    {
        return "00" . $i;
    }
    if ($i < 1000)
    {
        return "0" . $i;
    }
    return $i;
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
$chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S ? 1 : 0;
unset($chk);
include_once( "class/function_3d.php" );
$conf = new Config();
$ui = new UI_3D(true);
$tpl = new Template2();
$tbl = $g_gametype_3d[$gametype]['en'];
$editok = 1;
if ($curruser['account'] != G_SUPERACCOUNT)
{
    $rs = getdb("select * from lotto_{$tbl} where open='" . date("Y-m-d") . "'");
    if (!$rs->eof && 0 - 3 < $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'])
    {
        $editok = 0;
    }
}
if (ispost())
{
    if ($editok == 1 && ( $show3D3S == 1 || $conf->get("calcbet_{$tbl}") == 0 ))
    {
        $stopbet_3d = $conf->get("stopbet_{$tbl}");
        if (getvar("stopbet_3d") != "" && $stopbet_3d != getvar("stopbet_3d"))
        {
            $stopbet_3d = getvar("stopbet_3d");
            $conf->set("stopbet_{$tbl}", $stopbet_3d);
        }
        if (getvar("startbet_3d") != "")
        {
            $conf->set("startbet_{$tbl}", d4(intval(getvar("startbet_3d"))));
        }
        if ($gametype == 10 || $gametype == 20)
        {
            if (3 <= intval(getvar("closebet_3d")))
            {
                $conf->set("preClose_{$tbl}", intval(getvar("closebet_3d")));
            }
        }
        else if (1 <= intval(getvar("closebet_3d")))
        {
            $conf->set("preClose_{$tbl}", intval(getvar("closebet_3d")));
        }
    }
    if (getvar("betmin_3d") != "")
    {
        $conf->set("betmin_{$tbl}", intval(getvar("betmin_3d")));
    }
    if (getvar("betmin2face_3d") != "")
    {
        $conf->set("betmin2face_{$tbl}", intval(getvar("betmin2face_3d")));
    }
    if (getvar("minbackbet_3d") != "")
    {
        $conf->set("minbackbet_{$tbl}", intval(getvar("minbackbet_3d")));
    }
    if (getvar("max_all_percent_3d") != "" && intval(getvar("max_all_percent_{$tbl}")) <= 100)
    {
        $conf->set("max_all_percent_{$tbl}", intval(getvar("max_all_percent_3d")));
    }
    if (getvar("edit_percent_wod") != "")
    {
        $conf->set("edit_percent_wod", intval($_POST['edit_percent_wod']));
    }
    if (getvar("show_paper_day") != "")
    {
        $conf->set("show_paper_day", intval($_POST['show_paper_day']));
    }
    $page_count_ag = intval(getvar("page_count_ag"));
    $page_count_ag = $page_count_ag == 11 ? 90 : $page_count_ag;
    if ($page_count_ag != 0)
    {
        $conf->set("page_max_count_ag", $page_count_ag);
    }
    if (getvar("page_count") != "")
    {
        if (intval($_POST['page_count']) == 11)
        {
            $conf->set("page_max_count", 20);
        }
        else
        {
            $conf->set("page_max_count", intval($_POST['page_count']));
        }
    }
    if (getvar("AdjustAmountMode") != "")
    {
        $conf->set("AdjustAmountMode", intval($_POST['AdjustAmountMode']));
    }
    if ($curruser['account'] == G_SUPERACCOUNT || $curruser['account'] == $conf->get("master_account"))
    {
        $ti = intval(getvar("ServerAdjustTime"));
        $conf->set("ServerAdjustTime", min(max($ti, 0), 180));
    }
    if (getvar("lotto_auto_3s") != "")
    {
        $conf->set("lotto_auto_{$tbl}", intval(getvar("lotto_auto_3s")));
    }
    $cnyDateS = getvar("cnyDateS");
    $cnyDateE = getvar("cnyDateE");
    if ($cnyDateS != "" && is_date($cnyDateS))
    {
        $conf->set("cnyDateS", $cnyDateS);
    }
    if ($cnyDateE != "" && is_date($cnyDateE) && $cnyDateS <= $cnyDateE)
    {
        $conf->set("cnyDateE", $cnyDateE);
    }
    if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255)
    {
        $conf->set("lotto_auto{$tbl}Msg", "");
    }
    if (isset($_COOKIE['g_language']) && $_COOKIE['g_language'] == "zh-cn")
    {
        $conf->set("news_th", safeHTML(getvar("message_th")));
        $conf->set("news_{$tbl}", safeHTML(getvar("news_3d")));
        $conf->set("news_th_{$tbl}", safeHTML(getvar("news_th_3d")));
    }
    else
    {
        $conf->set("news_th", safeHTML(getvar("message_th")));
        $conf->set("news_{$tbl}", safeHTML(getvar("news_3d")));
        $conf->set("news_th_{$tbl}", safeHTML(getvar("news_th_3d")));
    }
    $color = getvar("color");
    if ($color != "")
    {
        $pattern = "^#(([a-fA-F0-9]{3})|([a-fA-F0-9]{6}))\$";
        if (ereg($pattern, $color))
        {
            $conf->set("newsClc", safeHTML(getvar("color")));
        }
    }
    else
    {
        $conf->set("newsClc", "#ff4040");
    }
    if (getvar("systemcheck_3d") != "")
    {
        $conf->set("systemcheck_{$tbl}", intval(getvar("systemcheck_3d")));
    }
    if (getvar("calcbet_3d") != "")
    {
        $conf->set("calcbet_{$tbl}", intval(getvar("calcbet_3d")));
    }
    if (getvar("sortstar_3d") != "")
    {
        $conf->set("protect_start_{$tbl}", intval(getvar("sortstar_3d")));
    }
    if (getvar("snow_open_3d") != "")
    {
        $conf->set("snow_open_{$tbl}", intval(getvar("snow_open_3d")));
    }
    if (getvar("editportmod_3d") != "")
    {
        $conf->set("editportmod_{$tbl}", intval(getvar("editportmod_3d")));
    }
    if (getvar("autoportmod_3d") != "")
    {
        $conf->set("autoportmod_{$tbl}", intval(getvar("autoportmod_3d")));
    }
    else
    {
        $conf->set("autoportmod_{$tbl}", 0);
    }
    if (getvar("systemcheck") != "")
    {
        $conf->set("systemcheck", intval(getvar("systemcheck")));
    }
    if (getvar("ipshow") != "")
    {
        $conf->set("ipshow", intval(getvar("ipshow")));
    }
    if ($chkGametype == 3)
    {
        if (getvar("bettype1_0_5h") != "")
        {
            $conf->set("bettype1_0_{$tbl}", intval(getvar("bettype1_0_5h")));
        }
        else
        {
            $conf->set("bettype1_0_{$tbl}", 0);
        }
    }
}
$tpl->set(array (
    "action",
    "?" . $_SERVER['QUERY_STRING'],
    "startbet_3d" => $conf->Get("startbet_{$tbl}"),
    "stopbet_3d" => $conf->Get("stopbet_{$tbl}")
));
$member_limit = intval($conf->Get("member_limit"));
switch ($show3D3S)
{
    case 0 :
        $tpl->set(array (
            "calcbet_3d" => $conf->Get("calcbet_{$tbl}"),
            "startbetStr" => "每期",
            "stopbetStr" => "每期截止"
        ));
        $tempStr = "已经";
        break;
    case 1 :
        $tpl->set(array ("startbetStr" => "每日", "stopbetStr" => "最後一期截止", "edit_exc" => "<tr><td colspan='2'><font color='red'>每期开/停盘时间请由「开奖」处修改</font></td></tr>"));
        $tpl->set("closebetStr", "<tr><td>每期关盘为开奖前</td><td><input name='closebet_3d' size=2 maxlength=2 value=" . intval($conf->get("preClose_{$tbl}")) . "> 分</td></tr>");
        $tempStr = "今日己有期数";
        break;
}
if ($editok == 0)
{
    $tpl->set("edit_exc", "<tr><td colspan='2'><font color='red'>注意，{$tempStr}颁奖无法变更时间！</font></td></tr>");
}
$tpl->set("betmin_3d", $conf->Get("betmin_{$tbl}"));
$tpl->set("betmin2face_3d", $conf->Get("betmin2face_{$tbl}"));
$tpl->set("minbackbet_3d", $conf->Get("minbackbet_{$tbl}"));
$tpl->set("max_all_percent_3d", $conf->Get("max_all_percent_{$tbl}"));
$tpl->set("systemcheck_CHK" . intval($conf->Get("systemcheck")), " checked ");
$tpl->set("ipshow_CHK" . intval($conf->Get("ipshow")), " checked ");
$tpl->set("floatBack3sStr", intval($conf->Get("floatBack_3s")) ? "可累加" : "不可累加" );
$tpl->set("member_num", $member_limit);
$tpl->set("message_th", $conf->Get("news_th"));
$tpl->set("news_3d", $conf->Get("news_{$tbl}"));
$tpl->set("news_th_3d", $conf->Get("news_th_{$tbl}"));
$tpl->set("newsClc", $conf->Get("newsClc") ? $conf->Get("newsClc") : "#ff4040" );
$systemcheck_3d = intval($conf->get("systemcheck_{$tbl}"));
$tpl->set("systemcheck" . $systemcheck_3d . "_3d", " checked");
$calcbet_3d = intval($conf->get("calcbet_{$tbl}"));
$tpl->set("calcbet" . $calcbet_3d . "_3d", "checked");
$sortstar_3d = intval($conf->get("protect_start_{$tbl}"));
$tpl->set("sortstar" . $sortstar_3d . "_3d", " checked");
$snow_open_3d = intval($conf->get("snow_open_{$tbl}"));
$tpl->set("snow_open" . $snow_open_3d . "_3d", " checked");
$editportmod_3d = intval($conf->get("editportmod_{$tbl}"));
$tpl->set("editportmod" . $editportmod_3d . "_3d", " checked");
$autoportmod_3d = intval($conf->get("autoportmod_{$tbl}"));
if ($autoportmod_3d == 1)
{
    $tpl->set("autoportmod_3d", " checked");
}
if ($chkGametype == 3)
{
    $bettype1_0_5h = intval($conf->get("bettype1_0_{$tbl}"));
    $tpl->set("bettype1_0_5hStr", "<label><input type='checkbox' value='1' name='bettype1_0_5h' " . ( $bettype1_0_5h ? "checked" : "" ) . ">双面盘操盘连动</label>");
}
$weekallname = array ("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "不限制", "不开放");
$editwods = "";
$y = $conf->get("edit_percent_wod");
$slcShow_paper_day = "";
$show_paper_day = $conf->get("show_paper_day");
$i = 0;
while ($i <= 8)
{
    $editwods .= "<option value='" . $i . "'";
    if ($i == $y)
    {
        $editwods .= " selected";
    }
    $editwods .= ">" . $weekallname[$i] . "</option>";
    $slcTemp = $i == $show_paper_day ? " selected" : "";
    $slcShow_paper_day .= "<option value='{$i}' {$slcTemp}>{$weekallname[$i]}</option>";
    ++$i;
}
$tpl->set("editwods", $editwods);
$tpl->set("show_paper_day", $slcShow_paper_day);
$weekallname = array ("", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "全部");
$editwods = "";
$page_max_count_ag = "";
$y = min(11, intval($conf->get("page_max_count")));
$y2 = min(11, intval($conf->get("page_max_count_ag")));
$i = 1;
while ($i <= 11)
{
    $editwods .= "<option value='{$i}'";
    if ($i == $y)
    {
        $editwods .= " selected";
    }
    $editwods .= ">" . $weekallname[$i] . "</option>";
    $slcTemp = $i == $y2 ? " selected" : "";
    $page_max_count_ag .= "<option value='{$i}' {$slcTemp}>{$weekallname[$i]}</option>";
    ++$i;
}
$tpl->set("pagecount", $editwods);
$tpl->set("page_count_ag", $page_max_count_ag);
$today = date("Y-m-d");
$minDate = getPaperMinDate(0 - 1, 1);
$page_count_date_str = "区间:{$minDate}～{$today}";
$minDate = getPaperMinDate(0 - 1);
$page_count_ag_date_str = "区间:{$minDate}～{$today}";
$tpl->set(array (
    "page_count_date_str" => $page_count_date_str,
    "page_count_ag_date_str" => $page_count_ag_date_str
));
$membermaxlimit = "";
if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255)
{
    $membermaxlimit = "&#12288;※ 本月份最大连线人数：<b>" . intval($conf->get("member_max_limit")) . "</b>";
}
$tpl->set("max_member_num", $membermaxlimit);
$tpl->set("tbl", $g_gametype_3d[$gametype]['ch']);
$AdjustAmountMode = intval($conf->get("AdjustAmountMode"));
$adjamtArr = array ("不调整", "百分比", "全归零");
$adjamtOpts = "";
$i = 0;
while ($i <= 2)
{
    $slcTemp = $i == $AdjustAmountMode ? " selected" : "";
    $adjamtOpts .= "<option value='{$i}' {$slcTemp}>{$adjamtArr[$i]}</option>";
    ++$i;
}
$tpl->set("AdjustAmountMode", $adjamtOpts);
$tpl->load("main", "admin_3d.htm");
if ($show3D3S)
{
    $tpl->set("show_if_3s_1", "");
    $tpl->set("show_if_3s_2", "");
}
else
{
    $tpl->set("show_if_3s_1", "<!--");
    $tpl->set("show_if_3s_2", "-->");
}
$lotto_auto_3s = $conf->get("lotto_auto_{$tbl}");
$tpl->set("lotto_auto" . $lotto_auto_3s . "_3s", " checked");
if ($curruser['account'] == G_SUPERACCOUNT || $curruser['account'] == $conf->get("master_account"))
{
    $ServerAdjustTime = 0;
    if ($conf->get("ServerAdjustTime") != "")
    {
        $ServerAdjustTime = $conf->get("ServerAdjustTime");
    }
    $tpl->set("ServerAdjustTime", $ServerAdjustTime);
}
else
{
    $tpl->set("show_if_super_1", "<!--");
    $tpl->set("show_if_super_2", "-->");
}
$tpl->set(array (
    "cnyDateS" => $conf->get("cnyDateS"),
    "cnyDateE" => $conf->get("cnyDateE")
));
$tpl->set("gametype", $gametype);
$ui->set("jslink", "<script type=\"text/javascript\" src=\"js_3d/farbtastic.js\"></script>");
$ui->set("csslink", "<link rel=\"stylesheet\" href=\"css/farbtastic.css\" type=\"text/css\" />");
$ui->header();
include( "admin_menu_3d.php" );
$tpl->show("main");
$ui->load("calendar_js", "calendar_js.htm");
$ui->show("calendar_js");
echo "\r\n<script type=\"text/javascript\">\r\n  \$(document).ready(function() {\r\n    \$(\"#picker\").farbtastic(\"#color\");\r\n  });\r\n </script>\r\n";
$ui->footer();